package org.jiamandan.com.strategy.listener;

import io.netty.channel.Channel;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jiamandan.com.config.UdpServerHandler;
import org.jiamandan.com.constant.DXConstant;
import org.jiamandan.com.dto.DXBasicDto;
import org.jiamandan.com.entity.RechargePile;
import org.jiamandan.com.service.IRechargePileService;
import org.jiamandan.com.strategy.IDXContextListener;
import org.jiamandan.com.utils.RedisUtil;
import org.springframework.stereotype.Service;


@Service(value = DXConstant.Service.CMD_F03)
@AllArgsConstructor
@Slf4j
public class DXF03UpgradeListener implements IDXContextListener {

    IRechargePileService iRechargePileService;
    RedisUtil redisUtil;

    @Override
       public void doExecute(DXBasicDto dto, Channel ctx) throws Exception {
        //log.info("接收嘉满电-DX获取0F3设备信息指令(CMD=0F3)【"+dto.getDeviceNo()+"】报文:" + JSON.toJSONString(dto));
        String payload = dto.getData();
        String result = payload.substring(36,38);
        //升级业务逻辑
        /*RechargePile rechargePile = iRechargePileService.getOne(Wrappers.<RechargePile>query().lambda()
                .eq(RechargePile::getPileNo, dto.getDeviceNo()));
        if(rechargePile!=null){
            rechargePile.setRemark(result.equals("00")?"升级成功":"升级失败");
            //todo  需要更新
            iRechargePileService.updateById(rechargePile);
        }*/
    }

    public void upgradeDevice(String data, Channel ctx){
        String substring = data.substring(data.length() - 6, 2);
        if(substring.equals("00")){
            //执行成功
            log.info("执行升级任务成功，当前偏移量={}");
        }else{
            log.info("执行升级任务失败，当前偏移量={}");
        }
    }

    private void sendMsg(String deviceNo, String version,Integer curPoint){
        DXBasicDto dto = new DXBasicDto();
         dto.setReserved("0000000000");
        dto.setProtocol("01");
        dto.setDeviceNo(deviceNo);
        String response = (String)redisUtil.hget("DX_UPGRADE_VALUE_" + dto.getDeviceNo() + "_" + version, curPoint.toString());
        UdpServerHandler.sendMessage(response,dto,"", DXConstant.Cmd.CMD_F03);
    }
}
